var V=new Vue({
    el:'#main',
    data:()=>({
        tabType:'PriceClass',
        PriceClass:[],//价格分类
        MerchDis:[],//业务员折扣
        pricing:[],//定价 
        copyPricing:[],//拷贝定价
        proClass:[],//产品分类
        selproclass:'0',//选择产品分类值
        selText:'',//搜索产品文本
        pritype:[],
        pageSize:10,//分页条数
        page:1,//分页
        editSorD:true,
        auth_ids:$('#adminauth_ids').text()
    }),
    methods:{
        formatUnit:formatUnit,
        //获取价格分类数据
        getPriceClass(){
            $('.loding').show()
            $.ajax({
                url:'/admin/price/list?request=price_type&page='+this.page+'&limit='+this.pageSize,
                type:'post',
                success:data=>{
                    data=JSON.parse(data)

                    this.PriceClass=data.data.data
                    this.page=data.data.page
                    if(data.data.pageSum > 0) this.Resetnav(this.page-0,data.data.pageSum,'#pagination')
                    $('.loding').hide()
                }
            })
        },
        //获取业务员折扣数据
        getMerchDis(){
           $('.loding').show()
           $.ajax({
            url:'/admin/price/list?request=price_discount&page='+this.page+'&limit='+this.pageSize,
            type:'post',
            success:data=>{
                data=JSON.parse(data)
                console.log(data)
                this.MerchDis=data.data.data
                this.page=data.data.page
                if(data.data.pageSum > 0) this.Resetnav(this.page-0,data.data.pageSum,'#MerchDis')
                $('.loding').hide()
            }
        })
           
        },
        //获取定价数据
        getpricing(){
            $('.loding').show()
            new Promise((res,rej)=>{
                $.ajax({
                    url:'/admin/price/list?request=price&page='+this.page+'&limit='+this.pageSize+'&proCateId='+this.selproclass+'&searchText='+this.selText,
                    type:'post',
                    success:data=>{                        
                        data=JSON.parse(data).data
                        this.pritype=data.price_type
                        this.pricing=data.price_list
                        this.page=data.page
                        if(data.pageSum) this.Resetnav(this.page-0,data.pageSum,'#pricing')         
                        $('.loding').hide()
                        this.$nextTick(()=>{
                            $("[data-toggle='tooltip']").tooltip();  
                        })
                        res()
                    }
                })
            }).then(()=>{
                 $.ajax({
                     url:'/admin/price/cateTypes',
                     type:'post',
                     success:data=>{
                         data=JSON.parse(data)
                         this.proClass=data
                     }
                 })
            })
        },
        findFun(){
            $.ajax({
                url:'/admin/price/list?request=price&proCateId='+this.selproclass+'&searchText='+this.selText,
                type:'post',
                success:data=>{
                    data=JSON.parse(data)
                    this.pritype=data.data.price_type
                    this.pricing=data.data.price_list
                    this.page=data.data.page
                    if(data.data.pageSum>0) this.Resetnav(this.page-0,data.data.pageSum,'#pricing')
                    else{
                        $('#pricing').hide()
                    }
                }
            })
        },
        priceAdd(){
            $.ajax({
                url:'/admin/price/priceadd',
                type:'post',
                data:{
                    data:this.pricing
                },
                success:data=>{
                    data=JSON.parse(data)
                    change_mes_type('.alert',data.status)
                    $('.alert').show()
                    $('.alert p').html('*'+data.msg+'<br>')
                }
            })
        },
        getFloat(num, n) {
            n = n ? parseInt(n) : 0;
            if(n <= 0) {
                return Math.round(num);
            }
            if(num<10){
                n++
            }
            num = Math.round(num * Math.pow(10, n)) / Math.pow(10, n); //四舍五入
            num = Number(num).toFixed(n); //补足位数
            return num;
        },
        //分页
        Resetnav(pageCurrent, pageSum,dom) {
            //数据总条数
            var myPageCount = pageSum;
            //一页显示多少条
            var myPageSize = this.pageSize;
            //总页数
            var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
            $.jqPaginator(dom, {
                totalPages: parseInt(countindex),
                //最多显示多少页
                visiblePages: 5,
                //默认显示第几页
                currentPage: pageCurrent,
                first: '<li class="first"><a href="javascript:;">'+langcon.page_home+'</a></li>',
                prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>'+langcon.previous_page+'</a></li>',
                next: '<li class="next"><a href="javascript:;">'+langcon.next_page+'<i class="arrow arrow3"></i></a></li>',
                last: '<li class="last"><a href="javascript:;">'+langcon.last_page+'</a></li>',
                page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                all:'<li class="page"><a href="javascript:;">'+myPageCount+' / '+this.page+'PG , '+this.pageSize+' /PG</a></li>',
                onPageChange:(num, type)=>{
                    if (type == "change") {
                        this.page=num
                        if(this.tabType=='PriceClass')
                        this.getPriceClass()
                        else if(this.tabType=='MerchDis')
                        this.getMerchDis()
                        else if(this.tabType=='pricing')
                        this.getpricing()
                    }
                }
            });
        },
        changeTab(type){
            this.page=0
            this.tabType=type
            if(type=='PriceClass')
            this.getPriceClass()
            else if(type=='MerchDis')
            this.getMerchDis()
            else if(type=='pricing')
            this.getpricing()
            $('.alert').hide()
        },
        autoFun(index){
            this.pricing[index].copyPrice_value=this.pricing[index].price_value.map(o => Object.assign({}, o))//深拷贝一份还原用
            this.pricing[index].price_value.forEach(element => {
                element.amount=this.getFloat(this.pricing[index].cif*(element.priceTypeAmount/100),2)
                if(element.priceTypeMoq){
                    element.moq=Math.ceil(element.priceTypeMoq*this.pricing[index].prochar_qty)
                }else{
                    element.moq=1
                }
                element.discount_range=element.priceTypeDiscount
            });
        },
        restore(index){
            if(this.pricing[index].copyPrice_value){
                this.pricing[index].price_value=Object.assign([],this.pricing[index].copyPrice_value)
            }
        },
        jumpFun(id){
            $.ajax({
                url:'/admin/album/fixedPrice?album_id='+id+'&request=requestAjax',
                type:'post',
                success:data=>{
                    data=JSON.parse(data).data
                    this.pritype=data.price_type
                    this.pricing=data.price_list
                    this.page=data.page
                    if(data.pageSum > 0) this.Resetnav(this.page-0,data.pageSum,'#pricing')         
                    $('.loding').hide()
                    this.$nextTick(()=>{
                        $("[data-toggle='tooltip']").tooltip();  
                    })
                }
            })
        },
        putprice(index,key,name,e){            
            if(e.target.value==0)
            this.pricing[index].price_value[key][name]=''
            else{
                if(name=='moq')
                this.pricing[index].price_value[key][name]=Math.ceil(e.target.value)
                else
                this.pricing[index].price_value[key][name]=e.target.value
            }

            this.$forceUpdate()
        },
        sortEdit(){
            this.pricing.sort((item1,item2)=>{
                if(this.editSorD)
                return item1.create_at-item2.create_at
                else
                return item2.create_at-item1.create_at
            })
            this.editSorD=!this.editSorD
        },
        delDis(id){
            $.ajax({
                url:'/admin/price/positionPriceDel?id='+id,
                type:'post',
                success:data=>{
                    data=JSON.parse(data)
                    change_mes_type('.alert',data.status)
                    $('.alert').show()
                    $('.alert p').html('*'+data.msg+'<br>')
                }
            })
        },
        // 價格分類刪除
        typeDeleteData(id,name){
            $.ajax({
                url:'/admin/price/typeDeleteData?id='+id+'&name='+name,
                type:'post',
                success:data=>{
                    data=JSON.parse(data)
                    change_mes_type('.alert',data.status)
                    $('.alert').show()
                    $('.alert p').html('*'+data.msg+'<br>')
                    if(data.status == 1) this.getPriceClass()
                }
            })
        }
    },
    mounted(){
        this.getPriceClass()
        $("#price_discount_Modal").on("hidden.bs.modal", function () {
            $(this).removeData("bs.modal");
            $(".modal-content").children().remove();
        });
        $("#price_discount_Modal").on("hidden.bs.modal", ()=>{
            if(localStorage.getItem("model")){
                this.getMerchDis()
                localStorage.removeItem("model");
            }
        });
        $("#price_type_Modal").on("hidden.bs.modal", function () {
            $(this).removeData("bs.modal");
            $(".modal-content").children().remove();
        });
        $("#price_type_Modal").on("hidden.bs.modal", event=>{
            if(localStorage.getItem("model")){
                this.getPriceClass()
                localStorage.removeItem("model");
            }
        });
        if($('#viewType').val()=='price'){
            this.tabType='pricing'
            this.jumpFun($('#album_id').val())
        }
    }
})